/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package src.j2ee.compumake.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import src.j2ee.compumake.dominio.Socket;

/************************************************************************
 **                                                                    **
 ** Nombre: DAOSocket.java                                             **
 ** Version: v1.0.0                                                    **
 ** Autor: amado.cante                                                 **
 ** Creado: 25/04/2013                                                 **
 ** Modificado:                                                        **
 ** Diagrama:                                                          **
 ** Caso de uso:                                                       **
 **                                                                    **
 ***********************************************************************/
public class DAOSocket {
    private static final String TABLA_SOCKETS = "Sockets";
    private static final String COLUMNA_ID_SOCKET = "idSocket";
    private static final String COLUMNA_SOCKET = "socket";
    private Connection conexion;
    private AccesoBaseDatos acceso = new AccesoBaseDatos();
    
    public DAOSocket(){
        this.conexion = this.acceso.abrirConexion();
    }
    
    public Socket buscaSocket(int idSocket){
        try{
            Statement sentencia = this.acceso.crearSentenciaBD(conexion);
            String query = "SELECT * FROM "+TABLA_SOCKETS+" WHERE "+COLUMNA_ID_SOCKET
                    +"='"+idSocket+"';";
            ResultSet resultado = sentencia.executeQuery(query);
            if(resultado.first()){
                String socket = resultado.getString(2);
                Socket s = new Socket(idSocket, socket);
                return s;
            }else{
                return null;
            }
        }catch(SQLException ex){
            return null;
        }
    }
    
    public ArrayList<Socket> listarsockets(){
        ArrayList<Socket> sockets = new ArrayList<>();
        try{
            Statement sentencia = acceso.crearSentenciaBD(conexion);
            String query = "SELECT * FROM "+TABLA_SOCKETS;
            ResultSet resultado = sentencia.executeQuery(query);
            while(resultado.next()){
                int idSocket = resultado.getInt(1);
                String socket = resultado.getString(2);
                Socket s = new Socket(idSocket, socket);
                sockets.add(s);
            }
            return sockets;
        }catch(SQLException ex){
            return sockets;
        }
    }
}
